package cn.shuto.liferay.plugin.chart.service.service.impl;

import java.util.Date;

import cn.shuto.liferay.plugin.chart.service.model.Option;
import cn.shuto.liferay.plugin.chart.service.service.base.OptionLocalServiceBaseImpl;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.search.Indexer;
import com.liferay.portal.kernel.search.IndexerRegistryUtil;
import com.liferay.portal.kernel.search.SearchException;
import com.liferay.portal.model.User;
import com.liferay.portal.service.ServiceContext;

/**
 * The implementation of the option local service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are
 * added, rerun ServiceBuilder to copy their definitions into the
 * {@link cn.shuto.liferay.plugin.chart.service.service.OptionLocalService}
 * interface.
 *
 * <p>
 * This is a local service. Methods of this service will not have security
 * checks based on the propagated JAAS credentials because this service can only
 * be accessed from within the same VM.
 * </p>
 *
 * @author shixw
 * @see cn.shuto.liferay.plugin.chart.service.service.base.OptionLocalServiceBaseImpl
 * @see cn.shuto.liferay.plugin.chart.service.service.OptionLocalServiceUtil
 */
public class OptionLocalServiceImpl extends OptionLocalServiceBaseImpl {
	/*
	 * NOTE FOR DEVELOPERS:
	 * 
	 * Never reference this interface directly. Always use {@link
	 * cn.shuto.liferay.plugin.chart.service.service.OptionLocalServiceUtil} to
	 * access the option local service.
	 */

	public Option addOption(String name,String description,long chartTemplateId,long dataSourceCfgId,String dataSql,
			ServiceContext serviceContext) throws SystemException,
			PortalException {

		long groupId = serviceContext.getScopeGroupId();
		long userId = serviceContext.getUserId();
		User user = userPersistence.findByPrimaryKey(userId);
		Date now = new Date();
		long optionId = counterLocalService.increment();
		Option option = optionPersistence.create(optionId);
		option.setUuid(serviceContext.getUuid());
		option.setUserId(userId);
		option.setGroupId(groupId);
		option.setCompanyId(user.getCompanyId());
		option.setUserName(user.getFullName());
		option.setCreateDate(serviceContext.getCreateDate(now));
		option.setModifiedDate(serviceContext.getModifiedDate(now));
		
		option.setName(name);
		option.setDescription(description);
		
		option.setChartTemplateId(chartTemplateId);
		option.setDataSourceId(dataSourceCfgId);
		
		option.setDataSql(dataSql);
		//更新索引
		Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
                Option.class);

		indexer.reindex(option);
		///////////////
		optionPersistence.update(option);
		return option;
	}
	public Option updateTemplateParamValue(long optionId,String paramValue,
			ServiceContext serviceContext) throws SystemException,
			PortalException {
		Date now = new Date();
		Option option = getOption(optionId);
		option.setModifiedDate(serviceContext.getModifiedDate(now));
		
		option.setTemplateParamValue(paramValue);
		
		optionPersistence.update(option);
		return option;
		
	}
	public Option updateOption(long optionId,String name,String description,long chartTemplateId,long dataSourceCfgId,String dataSql,
			ServiceContext serviceContext) throws SystemException,
			PortalException {

		long groupId = serviceContext.getScopeGroupId();
		long userId = serviceContext.getUserId();
		User user = userPersistence.findByPrimaryKey(userId);
		Date now = new Date();
		
		Option option = getOption(optionId);

		option.setUserId(userId);
		option.setGroupId(groupId);
		option.setCompanyId(user.getCompanyId());
		option.setUserName(user.getFullName());
		option.setModifiedDate(serviceContext.getModifiedDate(now));

		option.setName(name);
		option.setDescription(description);
		
		option.setChartTemplateId(chartTemplateId);
		option.setDataSourceId(dataSourceCfgId);
		
		option.setDataSql(dataSql);
		//更新索引
		Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
                Option.class);

		indexer.reindex(option);
		///////////////
		optionPersistence.update(option);
		return option;
	}
	
	@Override
	public Option deleteOption(Option option) throws SystemException {
		//更新索引
		Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
                Option.class);

		try {
			indexer.reindex(option);
		} catch (SearchException e) {
			e.printStackTrace();
		}
		///////////////
		return super.deleteOption(option);
	}
}
